package com.sherlocky.util;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONSerializer;

import com.rework.joss.persistence.convention.ConventionUtils;

public abstract class WebMessage {
	public interface RequestName {
		// 成功信息
		String SUCCESS_MESSAGE = "successMessage";

		// 错误信息
		String ERROR_MESSAGE = "errors";
	}
	/**
	 * 设置成功信息
	 * 
	 * @param request
	 * @param successMessages
	 */
	public static void setSuccessMessage(HttpServletRequest request,
			List<String> successMessages) {
		request.setAttribute(RequestName.SUCCESS_MESSAGE, successMessages);
	}
	/**
	 * 设置成功信息
	 */
	public static void setSuccessMessage(HttpServletRequest request,
			String successMessage) {
		setSuccessMessage(request, ConventionUtils.toList(successMessage));
	}
	
	/**
	 * 增加成功信息
	 */
	public static void addSuccessMessage(HttpServletRequest request,
			String successMessage) {
		addContent2Request(request, successMessage, RequestName.SUCCESS_MESSAGE);
	}

	/**
	 * 增加成功信息
	 */
	public static void addSuccessMessage(HttpServletRequest request,
			List<String> successMessages) {
		addList2Request(request, successMessages, RequestName.SUCCESS_MESSAGE);
	}

	/**
	 * 设置失败信息
	 */
	public static void setErrorMessage(HttpServletRequest request, List<String> errors) {
		request.setAttribute(RequestName.ERROR_MESSAGE, errors);
	}
	
	public static void setErrorMessage(HttpServletRequest request, String error) {
		setErrorMessage(request, ConventionUtils.toList(error));
	}
	
	private static void addList2Request(HttpServletRequest request,
			List<String> contents, String requestName) {
		List<String> list = (List<String>) request.getAttribute(requestName);
		if (list == null) {
			list = new ArrayList<String>();
		}
		list.addAll(contents);
		request.setAttribute(requestName, list);
	}
	
	private static void addContent2Request(HttpServletRequest request,
			String content, String requestName) {
		List<String> list = (List<String>) request.getAttribute(requestName);
		if (list == null) {
			list = new ArrayList<String>();
		}
		list.add(content);
		request.setAttribute(requestName, list);
	}
	
	public static void sendMessage(HttpServletResponse response, String str) throws Exception{
		response.setContentType("text/html; charset=utf-8");
		PrintWriter writer = response.getWriter();
		writer.println(str);
		writer.close();
		response.flushBuffer();
	}
	public static void sendJsonMessage(HttpServletResponse response, Map str) throws Exception{
		response.setContentType("text/html; charset=utf-8");
		PrintWriter writer = response.getWriter();
		writer.println("["+JSONSerializer.toJSON(str)+"]");
		writer.close();
		response.flushBuffer();
	}
	public static void sendJsonMessage(HttpServletResponse response,Object obj) throws Exception{
		response.setContentType("text/html; charset=utf-8");
		PrintWriter writer = response.getWriter();
		writer.println(JSONSerializer.toJSON(obj));
		writer.close();
		response.flushBuffer();
	}
}
